package com.oasistudio.ads.expert.banner;

import android.app.Activity;
import android.widget.LinearLayout;
import cn.domob.android.ads.DomobAdListener;
import cn.domob.android.ads.DomobAdManager;
import cn.domob.android.ads.DomobAdView;

import com.oasistudio.ads.expert.R;

public class AdViewDomob extends DomobAdView{
	DomobAdRequestListener adRequestListener;
	Activity activity;
	public static final String UNION = "domob.cn";
	public AdViewDomob(Activity activity){
		super(activity);
		this.activity = activity;
		this.adRequestListener = new DomobAdRequestListener(activity, UNION);
		
		addDomobAd();
	}
	
	//doesn't work
//	public void requestFreshAd(){
//		System.out.println("AdViewDomob.requestFreshAd()");
//		this.adRequestListener.beforeAdRequest();
//		super.requestFreshAd();
//	}
	
	/************************** www.domob.cn start *************************/
	private void addDomobAd() {
		LinearLayout adContainer = (LinearLayout) activity.findViewById(R.id.ads_container);
		
		adContainer.addView(new TitleBuilder().createTitle(activity, UNION));

		DomobAdManager.setIsTestMode(false);	
		LinearLayout domobContainer = new LinearLayout(activity);
		adContainer.addView(domobContainer);

		DomobAdManager.setPublisherId("56OJyGdIuMLYLYgZkb");	
		this.setAdListener(this.adRequestListener);
		this.setRequestInterval(20);
		domobContainer.addView(this, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
	}

	/************************** www.domob.cn end *************************/
	class DomobAdRequestListener extends AbstractAdsExpertAdListener implements DomobAdListener{
		public DomobAdRequestListener(Activity activity, String union){
			super(activity, union);
		}

		@Override
		public void onFailedToReceiveFreshAd(DomobAdView arg0) {
			// TODO Auto-generated method stub
			this.fail();
		}

		@Override
		public void onLandingPageClose() {
			// TODO Auto-generated method stub
			System.out.println("AdViewDomob.DomobAdListener.onLandingPageClose() ");
		}

		@Override
		public void onLandingPageOpening() {
			// TODO Auto-generated method stub
			System.out.println("AdViewDomob.DomobAdListener.onLandingPageOpening() ");
		}

		@Override
		public void onReceivedFreshAd(DomobAdView adView) {
			// TODO Auto-generated method stub
			this.succeed();
			AdHistory.saveAd(UNION, adView);
		}

	}


}
